package cn.org.intelli.lims.o2oa.model;

import cn.org.intelli.lims.o2oa.constant.MaterialStatus;
import java.util.Date;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.sub_erupt.Drill;
import xyz.erupt.annotation.sub_erupt.Filter;
import xyz.erupt.annotation.sub_erupt.Link;
import xyz.erupt.annotation.sub_field.Edit;
import xyz.erupt.annotation.sub_field.EditType;
import xyz.erupt.annotation.sub_field.Readonly;
import xyz.erupt.annotation.sub_field.View;
import xyz.erupt.annotation.sub_field.ViewType;
import xyz.erupt.annotation.sub_field.sub_edit.ChoiceType;
import xyz.erupt.annotation.sub_field.sub_edit.ReferenceTableType;
import xyz.erupt.annotation.sub_field.sub_edit.Search;

@Getter
@Setter
@Entity
@Table(name = "lims_material")
@SQLDelete(sql = "update lims_material set deleted = true, delete_time = now() where id = ?")
@Erupt(name = "耗材信息", dataProxy = Material.class, filter = @Filter("Material.deleted = false"), drills = {@Drill(title = "耗材使用记录", link = @Link(linkErupt = MaterialOp.class, joinColumn = "material.id"))})
public class Material implements DataProxy<Material> {
    @Id
    @EruptField(views = @View(title = "标签", type = ViewType.QR_CODE))
    private String id;
    @ManyToOne
    @EruptField(views = @View(title = "物品信息", column = "name"), edit = @Edit(title = "物品信息", readonly = @Readonly(add = false), type = EditType.REFERENCE_TABLE, referenceTableType = @ReferenceTableType(id = "id", label = "name")))
    private MaterialDesc desc;
    @EruptField(views = @View(title = "状态"), edit = @Edit(title = "状态", type = EditType.CHOICE, choiceType = @ChoiceType(fetchHandler = MaterialStatus.Handler.class), search = @Search()))
    private String status;
    @ManyToOne
    @EruptField(views = @View(title = "存储位置信息", column = "name"), edit = @Edit(title = "存储位置信息", type = EditType.REFERENCE_TABLE, referenceTableType = @ReferenceTableType(id = "id", label = "name")))
    private Locker locker;
    @Column(name = "delete_time")
    private Date deleteTime;
    private Boolean deleted = false;
    @EruptField(views = @View(title = "余量"), edit = @Edit(title = "余量"))
    private Integer remainder;
    @EruptField(views = @View(title = "计量单位"), edit = @Edit(title = "计量单位"))
    private String unit;

    @Override
    public void beforeAdd(Material material) {
        if (material.getId() == null) {
            material.setId(UUID.randomUUID().toString());
        }
    }
}
